import { _decorator, Button, Label, tween, Tween, UIOpacity, v3 } from 'cc';
import { Ab_BasePopUp_bA } from '../../../A-FRAME/component/ui.pop-up';
import { Ab_ClientAPI_bA } from '../../api/client.api';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import { Ab_Popup_bA } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_Setting_bA')
export class Ab_Setting_bA extends Ab_BasePopUp_bA<typeof Ab_Popup_bA, null> {
	protected Ab_default_return_bA: null;

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_renderLocalUI_bA();
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_Setting_bA, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			Ab_BasePopUp_bA.Ab_jelly_leave_bA(this.node).then(()=>{
				this.Ab_close_bA(null);
				btn.interactable = true;
			});
		},
		bgm(){
			let pick = !Ab_GameDC_bA.Ab_User_bA.Ab_bgm_bA;
			Ab_GameDC_bA.Ab_User_bA.Ab_bgm_bA = pick;
			this.Ab_setPickUI_bA(pick);
		},
		['user-info'](){
			Ab_ClientAPI_bA.Ab_SetClipboard_bA(Ab_GameDC_bA.Ab_Status_bA.Ab_deviceID_bA);
		}
	}

	public Ab_onShow_bA(){
		this.Ab_setPickUI_bA(Ab_GameDC_bA.Ab_User_bA.Ab_bgm_bA);
		Ab_BasePopUp_bA.Ab_jelly_enter_bA(this.node);
	}

	private Ab_renderLocalUI_bA(){
		(<const>[
			['lbl-title', 36],
			['lbl-bgm', 37],
			//['lbl-agreement', 38],
			//['lbl-privacy', 39],
		]).forEach(([name, idx])=>this.node.getChildByName(name).getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(idx));

		this.node.getChildByName('lbl-nickname').getComponent(Label).string = Ab_GameDC_bA.Ab_User_bA.Ab_nickname_bA;
		this.node.getChildByName('lbl-level').getComponent(Label).string = Ab_GameDC_bA.Ab_User_bA.Ab_level_bA.toString();
		this.node.getChildByName('lbl-id').getComponent(Label).string = Ab_GameDC_bA.Ab_Status_bA.Ab_deviceID_bA;
	}

	private Ab_pick_handle_position_bA = v3();
	private Ab_setPickUI_bA(pick: boolean){
		let type = pick ? 'on' : 'off';

		let duration = 0.1;

		let node_pick = this.Ab___btn_map___bA.get('bgm').node.getChildByName('pick');
		['floor', 'handle'].forEach(name=>{
			node_pick.getChildByName(name)
			.getComponentsInChildren(UIOpacity)
			.forEach(opt=>{
				Tween.stopAllByTarget(opt);
				opt.node.setPosition(0, 0, opt.node.position.z);
				tween(opt)
				.to(duration, {opacity: opt.node.name==type ? 255 : 0})
				.start();
			});
		});

		let node_handle = node_pick.getChildByName('handle');
		this.Ab_pick_handle_position_bA.set(0, 0, node_handle.position.z);
		this.Ab_pick_handle_position_bA.x = pick ? 30 : -30;

		Tween.stopAllByTarget(node_handle);
		tween(node_handle)
		.to(duration, {position: this.Ab_pick_handle_position_bA})
		.start();
	}
}

